package com.hzp.day51.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hzp.day51.biz.EmployeeBiz;
import com.hzp.day51.biz.PositionBiz;
import com.hzp.day51.model.Employee;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
	@Resource
	private EmployeeBiz employeeBiz;
	@Resource
	private PositionBiz positionBiz;
	@RequestMapping("/list")
	public void list(Map<String, Object> request){
		request.put("list", employeeBiz.findAll());
	}
	@RequestMapping("/add")
	public void preAdd(Map<String , Object> request){
		System.out.println("pppppp");
		request.put("list", positionBiz.findAll());
	}
	@RequestMapping("/update/{empId}")
	public String preUpdate(@PathVariable("empId") int id, Map<String, Object> request){
		request.put("emp",employeeBiz.findById(id));
		request.put("list", positionBiz.findAll());
		return "/employee/update";
	}
	@RequestMapping("/doAdd")
	public String add(Employee emp){
		employeeBiz.save(emp);
		return "redirect:list";
	}
	@RequestMapping("/doUpdate")
	public String update(Employee emp){
		emp.setPos(positionBiz.findById(emp.getPos().getPosId()));
		employeeBiz.update(emp);
		return "redirect:list";
	}
	@RequestMapping("/delete/{empId}")
	public String delete(@PathVariable("empId") int id){
		employeeBiz.delete(id);
		return "redirect:/employee/list";
	}
}
